using System;
using System.Collections.Generic;
using System.Text;

namespace BlackjackGUI
{
   class CPlayer
   {
      private int money;
      private LinkedList<CHand> m_hands;

      internal CPlayer()
      {
         money = 100;
         m_hands = new LinkedList<CHand>();
         m_hands.AddFirst(new CHand());
      }

      ~CPlayer()
      {
         for (int i = 0; i < m_hands.Count; i++)
         {
            m_hands.RemoveLast();
         }
         m_hands = null;
      }

      internal void AddMoney(int cash)
      {
         money += cash;
      }

      internal int AmtMoney()
      {
         return money;
      }

      internal bool Bet(int amt)
      {
         if (money >= amt)
         {
            money -= amt;
            return true;
         }
         return false;
      }

      internal void Clear()
      {
         for (int i = 0; i < m_hands.Count; i++)
         {
            m_hands.RemoveLast();
         }
         m_hands = null;
         m_hands = new LinkedList<CHand>();
         m_hands.AddFirst(new CHand());
      }

      internal void Add(int hand, CCard newCard)
      {
         //if (m_hands.Count == 0)
         //{
         //   m_hands.AddFirst(new LinkedList<CCard>());
         //}
         LinkedListNode<CHand> iter = m_hands.First;
         for (int ii = 0; ii < hand; ii++)
         {
            iter = iter.Next;
         }
         iter.Value.AddCard(newCard);
      }

      //internal CHand GetHand(int hand)
      //{
      //   return this[hand];
      //}


      public CHand this[int index]
      {
         get
         {
            if (index > m_hands.Count)
            {
               // TODO
               // throw new exception
            }

            LinkedListNode<CHand> handIterator = m_hands.First;
            for (int ii = 0; ii < index; ii++)
            {
               handIterator = handIterator.Next;
            }
            return handIterator.Value;
         }
      }
	

      //internal CCard GetCard(int hand, int card)
      //{
      //   if(hand > m_hands.Count)
      //   {
      //      // TODO
      //      // throw new exception
      //   }

      //   LinkedListNode<CHand> handIterator = m_hands.First;
      //   for (int ii = 0; ii < hand; ii++)
      //   {
      //      handIterator = handIterator.Next;
      //   }
      //   if (card > handIterator.Value.Count)
      //   {
      //      // throw argumentoutofrange exception
      //   }

      //   //LinkedListNode<CCard> cardIterator = handIterator.Value.First;
      //   //for (int jj = 0; jj < card; jj++)
      //   //{
      //   //   cardIterator = cardIterator.Next;
      //   //}
      //   return handIterator.Value[card];
      //}

      internal int NumHands()
      {
         return m_hands.Count;
      }

      //internal int NumCardsInHand(int hand)
      //{
      //   if (hand > m_hands.Count)
      //   {
      //      // throw new exception
      //   }
      //   return GetHand(hand).Count;
      //}

      internal bool Splitable(int hand)
      {
         if (this[hand].Count == 2)
         {
            ValueEnum first = this[hand][0].GetValue();
            ValueEnum second = this[hand][1].GetValue();
            if ((first == second) || 
                ((first == ValueEnum.TEN || first == ValueEnum.JACK || 
                  first == ValueEnum.QUEEN || first == ValueEnum.KING) && 
                  (second == ValueEnum.TEN || second == ValueEnum.JACK || 
                   second == ValueEnum.QUEEN || second == ValueEnum.KING)))
            {
               return true;
            }
         }
         return false;
      }

      // Pre:  hand is index of the hand to split
      internal void Split(int hand)
      {
         CHand newHand = new CHand();
         newHand.Bet = this[hand].Bet;
         newHand.AddCard(this[hand][1]);
         this[hand].RemoveLast();
         m_hands.AddLast(newHand);
      }

      #region GUI Stuff
      internal void ResizeCard(int hand, int card, int x, int y)
      {
         this[hand][card].MoveCard(x, y);
      }

      internal void AlignCards()
      {
         int numHands = NumHands();
         if (numHands == 0)
         {
            return;
         }
         else if (numHands == 1)
         {
            for (int ii = 0; ii < this[0].Count; ii++)
            {
               this[0][ii].MoveCard(350 + 40 * ii, 400 - 20 * ii);
            }
         }
         else if (numHands == 2)
         {
            for (int ii = 0; ii < this[0].Count; ii++)
            {
               this[0][ii].MoveCard(250 + 40 * ii, 400 - 20 * ii);
            }
            for (int ii = 0; ii < this[1].Count; ii++)
            {
               this[1][ii].MoveCard(550 + 40 * ii, 400 - 20 * ii);
            }
         }
         else if (numHands == 3)
         {
            for (int ii = 0; ii < this[0].Count; ii++)
            {
               this[0][ii].MoveCard(100 + 40 * ii, 400 - 20 * ii);
            }
            for (int ii = 0; ii < this[1].Count; ii++)
            {
               this[1][ii].MoveCard(400 + 40 * ii, 400 - 20 * ii);
            }
            for (int ii = 0; ii < this[2].Count; ii++)
            {
               this[2][ii].MoveCard(700 + 40 * ii, 400 - 20 * ii);
            }
         }
         else
         {
            throw new Exception("Does Not Handle this many hands! Please Contact Pete...");
         }
      }
      #endregion

   }
}
